/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 *
 * Copyright 2010 yura, Version 2.0 (the "License");
 *
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 *
 * distributed under the License is distributed on an "AS IS" BASIS,
 *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

package org.opu.doc2db.document.datadocument;

import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Arrays;
import javax.xml.parsers.ParserConfigurationException;
import org.opu.doc2db.document.datadocument.io.DefaultDocumentReader;
import org.opu.doc2db.document.datadocument.io.DefaultDocumentWriter;
import org.opu.doc2db.document.datadocument.io.DocumentWriter;

/**
 *
 * @author yura
 */
public class Main extends SimpleDocumentGenerator {

    /**
     * @param args the command line arguments
     * @throws IOException if any i/o errors.
     */
    public static void main(String[] args) throws IOException {

        /*
        Document doc = new Document();
        TextBlock tb = new TextBlock("firstText");
        tb.setProperty(TextBlock.PROPERTY_COLOR, "ffffff");
        tb.setProperty(TextBlock.PROPERTY_FONT_STYLE_BOLD, "true");
        doc.elementsAdd(new TextParagraph(Arrays.asList(tb)));
        doc.elementsAdd(toPar("text 2.1", " text 2.2"));

        doc.elementsAdd(toTable(
                toRow(toCel(new Metric<Integer>(10, "cell11"), toPar("cell11")),
                        toCel(new Metric<Integer>(20), toPar("cell12")),
                        toCel(new Metric<Integer>(30, "cell13"), toPar("cell13"))),
                toRow(new Metric<Integer>(44, "row 2"),
                        toCel(toPar("cell21")), toCel(toPar("cell22")), toCel(toPar("cell23"))),
                toRow(new Metric<Integer>(55),
                        toCel(toPar("cell31")), toCel(toPar("cell32")), toCel(toPar("cell33")))
                ));
        doc.elementsAdd(toPar("last text"));

        DocumentWriter w = new DefaultDocumentWriter(doc);
        w.write(new File("/home/yura/Desktop/doc3.xml"));
        
        DefaultDocumentReader r = new DefaultDocumentReader();
        doc = r.parseDocument(new File("/home/yura/Desktop/doc3.xml"));

        w.write(new File("/home/yura/Desktop/doc4.xml"));

        if (true) return;
        //*/

        Properties prop = new Properties();
        try {
            Class<Main> aClass = Main.class;
            String prpAdr = "/org/opu/doc2db/document/datadocument/global.properties";
            InputStream resourceStream = aClass.getResourceAsStream(prpAdr);
            prop.load(resourceStream);
        } catch (IOException ex) {
            System.err.println("fatal error on load properties: " + ex);
            System.exit(1);
        }
        StringBuilder sb = new StringBuilder();

        for (String str : args) {
            if (str.equals("enumeration")) {
                Enumeration<?> pn = prop.propertyNames();
                while (pn.hasMoreElements()) {
                    sb.append(pn.nextElement()).append(';');
                }
                break;
            }
            final Enumeration<?> propertyNames = prop.propertyNames();
            while (propertyNames.hasMoreElements()) {
                Object object = propertyNames.nextElement();
                if (str.equals(object)) {
                    sb.append(prop.getProperty(object.toString())).append(';');
                }
            }
        }

        String str = sb.toString();
        if (str.isEmpty()){
            System.out.println(prop);
        } else {
            System.out.println(str);
        }
    }
}
